package router

import (
	"github.com/astaxie/beego/logs"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"wonlicsys/api/v1"
	"wonlicsys/api/v1/Api/ding"
	"wonlicsys/middleware"
	"wonlicsys/router/admin"
	"wonlicsys/router/api"
)

//set routers
func InitRouter() *gin.Engine {
	router := gin.Default()
	// set recovery middleware
	if v1.Conf.RunMode == "pro" {
		router.Use(gin.Recovery())
	}
	//基础路由

	router.Handle("POST", "/", ding.CallBack)

	baseGroup := router.Group("/licSys/v1")
	/*--------------------------admin接口---------------------------------------*/
	adminGroup := baseGroup.Group("/admin")
	admin.AdminUser(adminGroup)
	admin.AdminUserNoAuth(adminGroup)

	/*--------------------------api接口---------------------------------------*/
	ApiGroup := baseGroup.Group("/api")
	ApiGroup.Use(middleware.JWTAuthMiddleware())
	api.BaseRoute(ApiGroup)
	api.OaRoute(ApiGroup)

	return router
}
func StartHttp() error {
	logs.Debug(v1.Conf, "配置信息")
	router := InitRouter()
	s := &http.Server{
		Addr:           v1.Conf.Host + ":" + v1.Conf.Port,
		Handler:        router,
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}

	//logs.Error(123123123, "错误--------->")
	if err := s.ListenAndServe(); err != nil {
		logs.Error(err)
		return err
	}
	return nil
}
